package com.hwz.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hwz.constant.MessageConstant;
import com.hwz.constant.StatusConstant;
import com.hwz.dto.DishDTO;
import com.hwz.dto.DishPageQueryDTO;
import com.hwz.entity.Dish;
import com.hwz.entity.DishFlavor;
import com.hwz.entity.Setmeal;
import com.hwz.exception.DeletionNotAllowedException;
import com.hwz.mapper.*;
import com.hwz.result.PageResult;
import com.hwz.result.Result;
import com.hwz.service.DishService;
import com.hwz.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Lenovo]
 * @version : [v1.0]
 * @createTime : [2024/3/13 20:32]
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 添加菜品
     * @param dishDTO
     */
    @Override
    @Transactional
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setStatus(StatusConstant.ENABLE);
        dishMapper.insert(dish);
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0)
        {
            flavors.forEach(flavor ->{
                flavor.setDishId(dishId);
            });
            dishFlavorMapper.insertFlavorList(flavors);
        }
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public DishVO getById(Long id) {
        Dish dish = dishMapper.selectById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectByDishId(id);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    public List<Dish> getByCategoryId(Long categoryId) {
        return dishMapper.selectByCategoryId(categoryId);
    }


    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> dishPage = dishMapper.getDishPage(dishPageQueryDTO);
        return new PageResult(dishPage.getTotal(), dishPage.getResult());
    }

    @Override
    public void updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);
        Long dishId = dishDTO.getId();
        dishFlavorMapper.deleteByDishId(dishId);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0)
        {
            flavors.forEach(flavor ->{
                flavor.setDishId(dishId);
            });
            dishFlavorMapper.insertFlavorList(flavors);
        }
    }

    @Override
    @Transactional
    public void deleteDish(List<Long> ids) {
        //判断当前菜品是否能够删除---是否存在起售中的菜品
        for (Long id:ids) {
            Dish dish = dishMapper.selectById(id);
            if (dish.getStatus() == StatusConstant.ENABLE)
            {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断当前菜品是否能够删除---是否被套餐关联了
        List<Long> setmealMapperByDishids = setmealDishMapper.getByDishids(ids);
        if (setmealMapperByDishids != null && setmealMapperByDishids.size() > 0) {
            //当前菜品被套餐关联了，不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品表中的菜品数据
        dishMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional
    public void startOrOff(Long id, Integer status) {
        Dish dish= Dish.builder().id(id).status(status).build();

        dishMapper.update(dish);
        if (status == StatusConstant.DISABLE) {
            // 如果是停售操作，还需要将包含当前菜品的套餐也停售
            List<Long> dishIds = new ArrayList<>();
            dishIds.add(id);
            // select setmeal_id from setmeal_dish where dish_id in (?,?,?)
            List<Long> setmealIds = setmealDishMapper.getByDishids(dishIds);
            if (setmealIds != null && setmealIds.size() > 0) {
                for (Long setmealId : setmealIds) {
                    Setmeal setmeal = Setmeal.builder()
                            .id(setmealId)
                            .status(StatusConstant.DISABLE)
                            .build();
                    setmealMapper.update(setmeal);
                }
            }
        }
    }

    @Override
    public List<DishVO> list(Long categoryId) {
        List<Dish> dishes = dishMapper.selectByCategoryId(categoryId);
        List<DishVO> dishVOS = new ArrayList<>();
        for (Dish dish:dishes) {
            if (dish.getStatus() == StatusConstant.ENABLE)
            {
                DishVO dishVO = new DishVO();
                BeanUtils.copyProperties(dish,dishVO);
                dishVO.setCategoryName(categoryMapper.getName(dish.getCategoryId()));
                List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(dish.getId());
                dishVO.setFlavors(flavors);
                dishVOS.add(dishVO);
            }
        }
        return dishVOS;
    }
}
